// Copyright (c) 2018-2022 The MobileCoin Foundation

#![doc = include_str!("../README.md")]
#![no_std]

use core::{
    fmt::{Debug, Formatter, Result as FmtResult},
    mem::size_of,
    ptr::read_unaligned,
    result::Result as StdResult,
};
use displaydoc::Display;
use mc_sgx_core_types::{IsvProductId, IsvSvn};
use sha2::{Digest, Sha256};

type Result = StdResult<Signature, Error>;

const ZEROV: [u8; 84] = [0; 84];
const HEADER1: [u8; 16] = [
    0x06, 0x00, 0x00, 0x00, 0xE1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
];
const HEADER2: [u8; 16] = [
    0x01, 0x01, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x60, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
];
const VENDOR_INTEL: [u8; 4] = [0x00, 0x00, 0x80, 0x86];
const VENDOR_OTHER: [u8; 4] = [0; 4];

/// The vendor enum used in the enclave
#[derive(Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[repr(u32)]
pub enum EnclaveVendor {
    Intel = 0x0000_8086,
    Other = 0x0,
}

impl TryFrom<u32> for EnclaveVendor {
    type Error = Error;

    fn try_from(src: u32) -> StdResult<EnclaveVendor, Self::Error> {
        match src {
            0x0000_8086 => Ok(EnclaveVendor::Intel),
            0x0 => Ok(EnclaveVendor::Other),
            _unknown => Err(Error::UnknownVendor),
        }
    }
}

/// An enumeration of errors which can occur while parsing a Signature struct
#[derive(Copy, Clone, Debug, Display, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub enum Error {
    /// Structure data has the wrong length
    Length,
    /// Invalid header value encountered
    BadHeader1,
    /// Invalid header value encountered
    BadHeader2,
    /// Unknown vendor encountered
    UnknownVendor,
    /// Enclave build date too old to be reasonable
    BadDate,
    /// Reserved bytes are not zero
    NonZeroReserved,
}

/// The length of the header1 field, in bytes
pub const HEADER_LEN: usize = 16;
/// The length of the header2 field, in bytes
pub const HEADER2_LEN: usize = 16;
/// The length of the SWDEFINED field, in bytes
pub const SWDEFINED_LEN: usize = 4;
/// The length of the MODULUS field, in bytes
pub const PUBKEY_LEN: usize = 384;
/// The length of the SIGNATURE field, in bytes
pub const SIGNATURE_LEN: usize = PUBKEY_LEN;
/// The length of the ENCLAVEHASH field, in bytes
pub const MRENCLAVE_LEN: usize = 32;
/// The length of the hash of the signer values, in bytes
pub const MRSIGNER_LEN: usize = 32;
/// The length of the "Q1" signature verification value
pub const Q1_LEN: usize = PUBKEY_LEN;
/// The length of the "Q2" signature verification value
pub const Q2_LEN: usize = PUBKEY_LEN;

const RESERVED1_LEN: usize = 84;
const RESERVED2_LEN: usize = 20;
const RESERVED3_LEN: usize = 32;
const RESERVED4_LEN: usize = 12;

/// The SIGSTRUCT structure
#[repr(C, packed)]
#[derive(Clone)]
pub struct Signature {
    /// The byte stream 0x06000000E10000000000010000000000
    header: [u8; 16],
    /// Intel enclaves will use 0x00008086, other enclaves will use 0x00000000
    vendor: [u8; 4],
    /// The build date of the enclave, as an 8-decimal date (YYYYMMDD) stored as
    /// little-endian bytes
    date: [u8; 4],
    /// The byte stream 0x01010000600000006000000001000000
    header2: [u8; 16],
    /// Data defined by the signature software
    swdefined: [u8; 4],
    /// Zeroes
    _reserved1: [u8; RESERVED1_LEN],
    /// The RSA 3072-bit public key
    modulus: [u8; 384],
    /// The RSA Exponent, as an LE u32
    exponent: [u8; 4],
    /// The RSA signature over the header and body
    signature: [u8; 384],
    /// The Extended SSA frame feature set selection, as 32 bits
    miscselect: [u8; 4],
    /// A bitmask of miscselect features, as 32 bits
    miscmask: [u8; 4],
    /// More zeroes
    _reserved2: [u8; RESERVED2_LEN],
    /// Enclave attribute flags
    attributes: [u8; 16],
    /// Bitmask of attribute flags
    attributemask: [u8; 16],
    /// The MRENCLAVE value, as bytes
    enclavehash: [u8; 32],
    /// Still more zeroes
    _reserved3: [u8; RESERVED3_LEN],
    /// The product ID, stored as little-endian bytes
    isvprodid: [u8; 2],
    /// The security version of the enclave, stored as little-endian bytes
    isvsvn: [u8; 2],
    /// Enough with the zeroes already
    _reserved4: [u8; RESERVED4_LEN],
    /// Q1 value
    q1: [u8; 384],
    /// Q2 value
    q2: [u8; 384],
}

impl Signature {
    /// Retrieve a reference to the header bytes
    pub fn header(&self) -> &[u8; HEADER_LEN] {
        &self.header
    }

    /// Retrieve the enclave vendor type.
    pub fn vendor(&self) -> EnclaveVendor {
        u32::from_le_bytes(self.vendor)
            .try_into()
            .expect("Signature not properly sanitized on load")
    }

    /// Retrieve the date (without timezone) the enclave was built as a
    /// [Binary-Coded Decimal](https://en.wikipedia.org/wiki/Binary-coded_decimal).
    ///
    /// This means that a date like April 15, 2020 will be encoded as the
    /// hexadecimal 0x20200415. It's a terrible way of encoding dates, but
    /// it's how SIGSTRUCT works internally.
    pub fn date(&self) -> u32 {
        u32::from_le_bytes(self.date)
    }

    /// Retrieve a reference to the secondary header bytes
    pub fn header2(&self) -> &[u8; HEADER2_LEN] {
        &self.header2
    }

    /// Retrieve the software-defined bytes
    pub fn swdefined(&self) -> &[u8; SWDEFINED_LEN] {
        &self.swdefined
    }

    /// Retrieve the signer's RSA public key, as bytes
    pub fn pubkey_bytes(&self) -> &[u8; PUBKEY_LEN] {
        &self.modulus
    }

    /// Retrieve the RSA exponent
    pub fn pubkey_exponent(&self) -> u32 {
        u32::from_le_bytes(self.exponent)
    }

    /// Retrieve a reference to the signature bytes
    pub fn signature(&self) -> &[u8; SIGNATURE_LEN] {
        &self.signature
    }

    /// Retrieve the miscellaneous selection flags
    pub fn misc_select(&self) -> u32 {
        u32::from_le_bytes(self.miscselect)
    }

    /// Retrieve which miscellaneous selection flags are relevant
    pub fn misc_mask(&self) -> u32 {
        u32::from_le_bytes(self.miscmask)
    }

    /// Retrieve the attributes flags (64-bit flags + 64-bit xfrm)
    pub fn attributes(&self) -> u128 {
        u128::from_le_bytes(self.attributes)
    }

    /// Retrieve a mask indicating which attribute flags are relevant
    pub fn attribute_mask(&self) -> u128 {
        u128::from_le_bytes(self.attributemask)
    }

    /// Retrieve a reference to the MRENCLAVE bytes
    pub fn mrenclave(&self) -> &[u8; MRENCLAVE_LEN] {
        &self.enclavehash
    }

    /// Generate the MRSIGNER value for this signature and return it as an array
    /// of bytes
    pub fn mrsigner(&self) -> [u8; MRSIGNER_LEN] {
        Sha256::digest(&self.modulus[..]).into()
    }

    /// Retrieve the product ID
    pub fn product_id(&self) -> IsvProductId {
        u16::from_le_bytes(self.isvprodid).into()
    }

    /// Retrieve the product version
    pub fn version(&self) -> IsvSvn {
        u16::from_le_bytes(self.isvsvn).into()
    }

    /// Retrieve a reference to the signature's "Q1" bytes
    pub fn q1(&self) -> &[u8; Q1_LEN] {
        &self.q1
    }

    /// Retrieve a reference to the signature's "Q2" bytes
    pub fn q2(&self) -> &[u8; Q2_LEN] {
        &self.q2
    }
}

impl Debug for Signature {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "Signature {{ ")?;
        write!(f, "header: {:02x?}, ", self.header)?;
        write!(f, "vendor: {:02x?}, ", self.vendor)?;
        write!(f, "date: {:02x?}, ", self.date)?;
        write!(f, "header2: {:02x?}, ", self.header2)?;
        write!(f, "swdefined: {:02x?}, ", self.swdefined)?;
        write!(f, "modulus: {:02x?}, ", &self.modulus[..])?;
        write!(f, "exponent: {:02x?}, ", self.exponent)?;
        write!(f, "signature: {:02x?}, ", &self.signature[..])?;
        write!(f, "miscselect: {:02x?}, ", self.miscselect)?;
        write!(f, "miscmask: {:02x?}, ", self.miscmask)?;
        write!(f, "attributes: {:02x?}, ", self.attributes)?;
        write!(f, "attributemask: {:02x?}, ", self.attributemask)?;
        write!(f, "enclavehash: {:02x?}, ", self.enclavehash)?;
        write!(f, "isvprodid: {:02x?}, ", self.isvprodid)?;
        write!(f, "isvsvn: {:02x?}, ", self.isvsvn)?;
        write!(f, "q1: {:02x?}, ", &self.q1[..])?;
        write!(f, "q2 {:02x?}, ", &self.q2[..])?;
        write!(f, "}}")
    }
}

impl Eq for Signature {}

impl PartialEq for Signature {
    fn eq(&self, other: &Self) -> bool {
        self.header == other.header
            && self.vendor == other.vendor
            && self.date == other.date
            && self.header2 == other.header2
            && self.swdefined == other.swdefined
            && self._reserved1[..] == other._reserved1[..]
            && self.modulus[..] == other.modulus[..]
            && self.exponent == other.exponent
            && self.signature[..] == other.signature[..]
            && self.miscmask == other.miscmask
            && (self.misc_select() & self.misc_mask()) == (other.misc_select() & other.misc_mask())
            && self._reserved2 == other._reserved2
            && self.attributemask == other.attributemask
            && (self.attributes() & self.attribute_mask())
                == (other.attributes() & other.attribute_mask())
            && self.enclavehash == other.enclavehash
            && self._reserved3 == other._reserved3
            && self.isvprodid == other.isvprodid
            && self.isvsvn == other.isvsvn
            && self.q1[..] == other.q1[..]
            && self.q2[..] == other.q2[..]
    }
}

impl TryFrom<&[u8]> for Signature {
    type Error = Error;

    fn try_from(src: &[u8]) -> Result {
        if src.len() < size_of::<Signature>() {
            return Err(Error::Length);
        }

        // *hurk*
        let retval = unsafe { read_unaligned(src.as_ptr() as *const Signature) };

        if retval.header[..] != HEADER1[..] {
            return Err(Error::BadHeader1);
        }

        if retval.vendor != VENDOR_INTEL && retval.vendor != VENDOR_OTHER {
            return Err(Error::UnknownVendor);
        }

        // SGX enclaves built during Obama's second term are not viable.
        if u32::from_le_bytes(retval.date) < 0x2017_0120 {
            return Err(Error::BadDate);
        }

        if retval.header2[..] != HEADER2[..] {
            return Err(Error::BadHeader2);
        }

        // All reserved are all zero
        for reserved in [
            &retval._reserved1[..],
            &retval._reserved2[..],
            &retval._reserved3[..],
            &retval._reserved4[..],
        ]
        .iter()
        {
            if *reserved != &ZEROV[..reserved.len()] {
                return Err(Error::NonZeroReserved);
            }
        }

        Ok(retval)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    const VALID: &[u8] = include_bytes!("valid.css");

    // values copied from the sgx_sign generated dump file at the time valid.css was
    // generated
    const VALID_PARSED: Signature = Signature {
        header: HEADER1,
        vendor: [0x0, 0x0, 0x0, 0x0],
        date: [0x28, 0x1, 0x20, 0x20],
        header2: HEADER2,
        swdefined: [0x0, 0x0, 0x0, 0x0],
        _reserved1: [0; RESERVED1_LEN],
        modulus: [
            0x5f, 0x37, 0xff, 0xfc, 0x94, 0xba, 0x99, 0xf0, 0xc6, 0x29, 0x44, 0xdf, 0x86, 0x30,
            0x40, 0x5e, 0x87, 0x48, 0xcb, 0x8e, 0xf3, 0x0a, 0x52, 0x50, 0x14, 0x65, 0x69, 0x4e,
            0x21, 0x41, 0xe7, 0x23, 0x4f, 0x07, 0x48, 0xd6, 0x80, 0x3c, 0x08, 0xf8, 0xb0, 0x24,
            0x86, 0x12, 0xf5, 0xcb, 0x61, 0xe6, 0xba, 0x33, 0x7a, 0x5b, 0x82, 0x1d, 0xb1, 0xb1,
            0x1e, 0xdc, 0x76, 0xa9, 0xdd, 0x91, 0xf8, 0x98, 0x34, 0x94, 0x4b, 0xf9, 0xce, 0xd4,
            0x7d, 0x09, 0x96, 0x0f, 0x6f, 0xe1, 0x1a, 0x5e, 0x7b, 0x64, 0xb9, 0x5a, 0x4d, 0xbb,
            0xa0, 0x77, 0xef, 0x55, 0xf6, 0x5b, 0x34, 0x67, 0xe9, 0x3a, 0x3e, 0xc4, 0x85, 0x65,
            0x0d, 0x82, 0x8e, 0x9b, 0xa5, 0x26, 0x34, 0x24, 0x43, 0xfe, 0xe3, 0xf2, 0x76, 0x1e,
            0xce, 0xa7, 0xd5, 0x23, 0x10, 0x36, 0xa3, 0xf4, 0xbc, 0xae, 0x32, 0x31, 0xbb, 0x4e,
            0x2d, 0xd2, 0x88, 0xca, 0xe5, 0x21, 0x2e, 0x26, 0xe5, 0xe8, 0x9c, 0x30, 0x48, 0xbf,
            0x5a, 0x9f, 0x0d, 0x69, 0x2a, 0xaa, 0x14, 0x94, 0x2b, 0xa3, 0x57, 0x4d, 0x5e, 0x5a,
            0xf1, 0x44, 0xb8, 0x50, 0x75, 0xd1, 0xd5, 0x2e, 0x6c, 0x37, 0xc6, 0xb2, 0xde, 0x1b,
            0x16, 0x32, 0xd0, 0x7a, 0x53, 0xa4, 0xc1, 0x05, 0x9c, 0x74, 0xb5, 0xc8, 0x73, 0xd5,
            0x04, 0xa9, 0x1a, 0xb4, 0xc9, 0x48, 0xe8, 0xa6, 0x01, 0x69, 0xaf, 0x61, 0x9a, 0xfd,
            0x35, 0x52, 0x1a, 0x86, 0x3b, 0x65, 0xdc, 0x90, 0xc6, 0xc2, 0xf9, 0xe2, 0x48, 0x12,
            0xc8, 0x51, 0xf0, 0xf2, 0x2d, 0xbe, 0x52, 0xf3, 0x5f, 0x3f, 0xd2, 0x60, 0x37, 0x32,
            0xfa, 0xcc, 0x01, 0xe7, 0xfd, 0xef, 0x9d, 0xa3, 0x38, 0xa5, 0x58, 0x97, 0x91, 0x52,
            0x6c, 0xe2, 0x9d, 0xe0, 0x71, 0x4b, 0x9e, 0x5c, 0x10, 0xc6, 0x7d, 0xd4, 0xf8, 0x64,
            0x06, 0x8f, 0xb7, 0xa4, 0x6c, 0xe0, 0x84, 0x62, 0x9d, 0x3e, 0xd5, 0x24, 0xe2, 0x0b,
            0x02, 0x34, 0x0d, 0xdb, 0x70, 0x98, 0x77, 0xdb, 0x2c, 0xd4, 0x22, 0xb7, 0xef, 0x1e,
            0xe4, 0x4d, 0xb1, 0xbe, 0x66, 0xbb, 0xf1, 0x75, 0x11, 0xad, 0x5c, 0x95, 0x53, 0x35,
            0x03, 0xdb, 0x88, 0x42, 0xa0, 0xa2, 0xc4, 0xe8, 0xa1, 0x26, 0xbc, 0x08, 0x91, 0x3a,
            0x3c, 0xc2, 0x0c, 0x66, 0x26, 0xed, 0x57, 0x76, 0xa3, 0xed, 0xaa, 0x37, 0x98, 0xed,
            0x73, 0x26, 0x21, 0x59, 0x09, 0xc3, 0xa7, 0xf0, 0x32, 0x6d, 0x53, 0x5e, 0xaa, 0xd8,
            0x58, 0x47, 0xb2, 0x4a, 0xc2, 0xbb, 0xa6, 0x11, 0x8d, 0x53, 0x8a, 0x32, 0x16, 0x02,
            0x44, 0x0c, 0xc4, 0xf0, 0xd4, 0x54, 0x14, 0xc7, 0xbb, 0xf8, 0x45, 0x24, 0x91, 0xc5,
            0xac, 0xcc, 0x84, 0x20, 0x19, 0xcd, 0x2d, 0xe8, 0x6f, 0xe5, 0x55, 0xf3, 0xa6, 0x5d,
            0xfb, 0x4d, 0x07, 0x67, 0x52, 0xa8,
        ],
        exponent: [0x03, 0x00, 0x00, 0x00],
        signature: [
            0x35, 0x52, 0xaf, 0x6f, 0x44, 0x7a, 0x40, 0x61, 0x64, 0x2e, 0x4e, 0xfe, 0xf7, 0x78,
            0x8e, 0xfa, 0xa0, 0xcc, 0xf7, 0x26, 0x8a, 0x5e, 0x25, 0xc8, 0x2d, 0x0b, 0x8c, 0x47,
            0x3b, 0x81, 0xab, 0x5e, 0xf3, 0x8a, 0x4f, 0xa2, 0xa7, 0x03, 0xc0, 0xd6, 0x2d, 0xee,
            0x34, 0xfd, 0xe0, 0xae, 0x93, 0xa2, 0xc8, 0x12, 0x0a, 0x34, 0x40, 0x10, 0x0d, 0x2c,
            0x83, 0x5c, 0xb3, 0x61, 0xa8, 0x18, 0x2d, 0xc7, 0x2a, 0x26, 0xed, 0x2e, 0x91, 0xa3,
            0xe2, 0xd1, 0xde, 0xca, 0xee, 0x78, 0xef, 0x5c, 0x28, 0xb9, 0xc6, 0x26, 0xe0, 0x52,
            0x09, 0xae, 0x5a, 0x8a, 0x28, 0x3c, 0x50, 0x13, 0x60, 0x1e, 0xbe, 0x9b, 0xea, 0x50,
            0x89, 0xd9, 0x66, 0x35, 0xe4, 0x98, 0x1e, 0x8d, 0xd2, 0x0a, 0x23, 0xfd, 0xbc, 0x16,
            0xd3, 0x82, 0x21, 0x8d, 0xf6, 0x33, 0xfd, 0x17, 0xff, 0x6d, 0x49, 0x78, 0x9d, 0x53,
            0x8a, 0x36, 0x98, 0x95, 0x40, 0xd0, 0x51, 0xc3, 0xac, 0x68, 0x4a, 0xc7, 0x0d, 0xfc,
            0xd5, 0xf5, 0xdc, 0xc2, 0xc6, 0xde, 0xa1, 0x0f, 0x0e, 0xbd, 0x0a, 0x61, 0xb2, 0xd5,
            0x58, 0x2f, 0x55, 0x86, 0x22, 0x63, 0xdd, 0xb7, 0x6f, 0x53, 0xfb, 0xbb, 0x37, 0x08,
            0xc4, 0xcf, 0x86, 0xf6, 0x79, 0x93, 0x49, 0x9c, 0xae, 0xa3, 0xa1, 0xfb, 0x73, 0x50,
            0xfc, 0x6f, 0x57, 0xf3, 0xd8, 0xd1, 0x2f, 0x4c, 0x91, 0xad, 0x1c, 0x4d, 0x93, 0xed,
            0x92, 0x38, 0x5c, 0x62, 0x3c, 0x87, 0x79, 0x0f, 0xfc, 0x69, 0xaa, 0xe4, 0xec, 0xcd,
            0xb1, 0x07, 0xfe, 0x71, 0xcf, 0xf2, 0xc5, 0xa6, 0x67, 0xd3, 0x21, 0x6a, 0x4d, 0x3a,
            0x3d, 0x67, 0xe8, 0xee, 0x70, 0x2f, 0xd6, 0x7a, 0x0a, 0x4e, 0x1a, 0xfb, 0x43, 0x04,
            0xa6, 0x75, 0xda, 0xa4, 0x62, 0x30, 0x26, 0xac, 0xfc, 0x28, 0xaa, 0xdf, 0x95, 0x62,
            0xf5, 0xd5, 0x4e, 0x84, 0x72, 0xa5, 0xb0, 0xd4, 0x75, 0xd7, 0xab, 0xe2, 0x8b, 0xd5,
            0x7f, 0x7c, 0x42, 0xd6, 0x75, 0xea, 0xa5, 0xda, 0x58, 0xa1, 0x7d, 0x18, 0xba, 0x71,
            0x6e, 0xaa, 0xc8, 0xeb, 0x45, 0xd4, 0xe5, 0x4e, 0x27, 0x40, 0xb8, 0x1e, 0xf8, 0xff,
            0x04, 0x8c, 0x4e, 0x8c, 0xdb, 0xb4, 0xd1, 0x78, 0x6f, 0xca, 0x7e, 0xbb, 0x85, 0x95,
            0xb7, 0x03, 0x2c, 0x5e, 0x81, 0xb2, 0x02, 0x6a, 0x35, 0x0f, 0x49, 0x01, 0xe6, 0x96,
            0x01, 0x02, 0xf5, 0xd3, 0x0d, 0x3a, 0x3a, 0x8a, 0x47, 0x54, 0x45, 0x48, 0xbe, 0x3c,
            0x3e, 0xce, 0x8f, 0xfb, 0x15, 0x35, 0x4e, 0x61, 0x74, 0x94, 0x86, 0xc2, 0x44, 0xa7,
            0x0c, 0x03, 0x8a, 0x69, 0x0b, 0x29, 0xd8, 0x38, 0x5d, 0xf9, 0xc4, 0x2c, 0x01, 0x97,
            0x03, 0x91, 0x7c, 0x03, 0xa8, 0x54, 0x4c, 0x11, 0x3b, 0x1f, 0x74, 0xae, 0xfe, 0x39,
            0x92, 0x16, 0xbe, 0xb5, 0xee, 0x8e,
        ],
        miscselect: [0, 0, 0, 0],
        miscmask: [0xff, 0xff, 0xff, 0xff],
        _reserved2: [0; RESERVED2_LEN],
        attributes: [0x4, 0, 0, 0, 0, 0, 0, 0, 0x3, 0, 0, 0, 0, 0, 0, 0],
        attributemask: [
            0xfd, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff,
            0xff, 0xff,
        ],
        enclavehash: [
            0x5b, 0x9f, 0xbf, 0xca, 0x6a, 0xc6, 0x2d, 0x82, 0x36, 0xec, 0x5e, 0x1c, 0x4e, 0xeb,
            0xd8, 0x47, 0xfb, 0x18, 0x07, 0x45, 0x0a, 0x78, 0xea, 0x24, 0x21, 0xbe, 0x5c, 0xb4,
            0xb4, 0x9a, 0x5e, 0xa9,
        ],
        _reserved3: [0; RESERVED3_LEN],
        isvprodid: [0x1, 0],
        isvsvn: [0x1, 0],
        _reserved4: [0; RESERVED4_LEN],
        q1: [
            0xdd, 0x09, 0xd4, 0xe7, 0x60, 0xad, 0x2a, 0x39, 0x38, 0x6e, 0x50, 0xb1, 0x0f, 0x92,
            0xaf, 0x13, 0xb5, 0x47, 0x14, 0xf7, 0x37, 0x11, 0xaf, 0x37, 0x33, 0x1b, 0xe1, 0x3f,
            0xc8, 0xfc, 0xc1, 0x27, 0xbb, 0xd0, 0x69, 0x52, 0x67, 0x61, 0x1c, 0x59, 0x05, 0x80,
            0x84, 0xde, 0xb6, 0x3d, 0x1c, 0x89, 0xbc, 0xbc, 0x84, 0x92, 0x3b, 0x58, 0x5f, 0x3f,
            0x7d, 0x97, 0x43, 0xc9, 0x64, 0x48, 0xb7, 0xcf, 0x15, 0xcc, 0x79, 0xec, 0xf9, 0xe8,
            0x84, 0x5a, 0x32, 0x82, 0xe0, 0x5c, 0x4e, 0xf4, 0x2f, 0x5c, 0x66, 0x1c, 0x51, 0xd1,
            0x50, 0x2c, 0x81, 0x99, 0xe1, 0xb3, 0xd9, 0xec, 0xb3, 0xfb, 0xfa, 0xcc, 0x96, 0x9a,
            0x2d, 0x8e, 0xde, 0xc4, 0xfc, 0x86, 0x21, 0xdd, 0x4d, 0x78, 0x9f, 0x76, 0xde, 0x64,
            0x5b, 0x2d, 0x92, 0x58, 0x52, 0x87, 0xae, 0xef, 0x38, 0xd7, 0xac, 0xa5, 0xaa, 0xa0,
            0x71, 0x1b, 0x67, 0xd3, 0x63, 0xed, 0x7c, 0xea, 0x3b, 0x35, 0xef, 0x76, 0xec, 0xee,
            0x1d, 0x3a, 0x1f, 0x24, 0x4a, 0xc7, 0x90, 0xfd, 0x37, 0x04, 0xf6, 0x3c, 0x57, 0xfb,
            0xad, 0x7a, 0xba, 0xad, 0xd6, 0xb3, 0xbb, 0x63, 0x8e, 0x0c, 0x4d, 0xf7, 0x85, 0x7f,
            0x38, 0x9f, 0x41, 0x33, 0xdf, 0x65, 0x26, 0xd6, 0xee, 0x29, 0x19, 0x4c, 0x25, 0x86,
            0x64, 0xd7, 0x21, 0x46, 0x8a, 0x2b, 0xac, 0xd4, 0xd1, 0xdd, 0x9c, 0x0b, 0x9f, 0x7b,
            0x86, 0xde, 0x51, 0x1e, 0x3e, 0x5a, 0x82, 0x54, 0xbb, 0xa9, 0x92, 0xb9, 0x5f, 0xd2,
            0x31, 0x77, 0x45, 0xf2, 0x91, 0xf3, 0x93, 0x20, 0x31, 0x7a, 0x57, 0x53, 0x07, 0xb0,
            0x4b, 0x16, 0x6c, 0xa7, 0xae, 0x1f, 0x4d, 0x11, 0x83, 0x89, 0xf0, 0x6f, 0x1a, 0xbc,
            0x4f, 0x97, 0x0a, 0xea, 0xcc, 0x2f, 0x98, 0xcf, 0x86, 0x78, 0xc7, 0x28, 0xdb, 0xba,
            0x9e, 0xb8, 0x36, 0x5d, 0xb1, 0x0f, 0xda, 0x71, 0xcf, 0x32, 0xc2, 0x14, 0x7e, 0x77,
            0xb0, 0x66, 0x04, 0x62, 0xa4, 0x98, 0x58, 0xed, 0xc0, 0x12, 0x85, 0xb9, 0x33, 0x09,
            0xef, 0xff, 0xb8, 0x0a, 0xe8, 0xfa, 0x95, 0x7a, 0x83, 0xb6, 0xef, 0x5e, 0x93, 0xe5,
            0x2d, 0xfd, 0x8f, 0xed, 0xbb, 0xfb, 0x4f, 0x45, 0xd8, 0xd2, 0xb3, 0x02, 0x78, 0xea,
            0x8e, 0xd9, 0x8a, 0xc8, 0x04, 0x56, 0xbf, 0x11, 0xce, 0x96, 0xaf, 0xea, 0x10, 0xd1,
            0x8e, 0x35, 0x80, 0x8e, 0x08, 0x46, 0xff, 0xf4, 0xc4, 0x2d, 0x65, 0x09, 0x14, 0xe2,
            0x03, 0x19, 0x5b, 0x03, 0x63, 0x21, 0x6b, 0x21, 0x76, 0x01, 0x17, 0xef, 0xdc, 0x7f,
            0xc6, 0x88, 0xe8, 0xdb, 0x9f, 0xf6, 0xd6, 0x36, 0x3e, 0x9a, 0xe6, 0xcc, 0x1e, 0x51,
            0x77, 0x5a, 0xfc, 0x35, 0xd9, 0xe1, 0x6b, 0x81, 0xe7, 0x8f, 0x46, 0xbf, 0xed, 0x5a,
            0xfc, 0x5e, 0xe1, 0x6b, 0x5f, 0x79,
        ],
        q2: [
            0x61, 0xb3, 0x86, 0xf7, 0x4a, 0xa0, 0x9a, 0xae, 0xf2, 0x18, 0x5c, 0x69, 0x72, 0xe4,
            0xed, 0xd5, 0x61, 0xfd, 0x1f, 0x54, 0xf1, 0xcc, 0xf1, 0x93, 0x9a, 0x1b, 0x85, 0x5a,
            0x5a, 0x09, 0x29, 0x1a, 0xb6, 0x50, 0x6d, 0x2d, 0x24, 0x1d, 0x2c, 0x07, 0x78, 0xac,
            0x2e, 0x9c, 0xf2, 0xc7, 0xd2, 0xc1, 0xcf, 0xe9, 0x4c, 0xd7, 0x44, 0x13, 0x93, 0xf7,
            0x1d, 0x06, 0xd8, 0x25, 0x69, 0x88, 0xe0, 0x98, 0xaf, 0x4f, 0xdb, 0xef, 0xf1, 0x0c,
            0x34, 0x83, 0x4f, 0x36, 0xbe, 0x86, 0x05, 0x4d, 0xe4, 0x98, 0xb3, 0xe7, 0xa9, 0xaa,
            0x60, 0x0c, 0xea, 0x55, 0xa1, 0xa0, 0xfd, 0x0d, 0x0a, 0xf6, 0x89, 0xad, 0x87, 0x6d,
            0xc0, 0x97, 0x73, 0xff, 0x1c, 0xb5, 0x92, 0xde, 0xc2, 0x1c, 0x1c, 0xa6, 0xf8, 0x2c,
            0x38, 0xc8, 0xa8, 0xbd, 0x19, 0x59, 0x80, 0x35, 0xec, 0x30, 0x76, 0x9c, 0x60, 0x5c,
            0x91, 0x48, 0x41, 0xa9, 0xfe, 0x81, 0xaf, 0x64, 0x22, 0x0c, 0x46, 0x06, 0x6c, 0x95,
            0x00, 0x57, 0x90, 0x33, 0x1d, 0x5e, 0x2b, 0x17, 0x2d, 0x3b, 0xc3, 0x72, 0x89, 0x4e,
            0xc7, 0x82, 0xb7, 0x1e, 0xb3, 0xa7, 0x1c, 0xcb, 0x33, 0xba, 0x44, 0xb5, 0x04, 0x73,
            0xef, 0x8c, 0x7a, 0x7e, 0x7d, 0xe0, 0xb0, 0x3d, 0x27, 0x2b, 0xa7, 0xb7, 0xb9, 0x67,
            0x79, 0xde, 0x7c, 0x53, 0xa6, 0x1e, 0xaf, 0x45, 0x47, 0xd6, 0x98, 0x47, 0xcc, 0xe0,
            0x1a, 0xe7, 0xf8, 0x0c, 0x8a, 0xf8, 0x40, 0x63, 0xef, 0xc9, 0x16, 0x46, 0x21, 0x07,
            0xad, 0xca, 0xf0, 0xd7, 0x20, 0xc3, 0x05, 0x70, 0x6f, 0xee, 0x42, 0xf8, 0xa9, 0x1e,
            0xda, 0xd4, 0x96, 0x51, 0x91, 0xe2, 0xcc, 0x41, 0x2a, 0x0e, 0xe8, 0x21, 0xa9, 0x1d,
            0x74, 0xd8, 0x4c, 0x25, 0x2a, 0xcc, 0xe2, 0x46, 0x5a, 0x51, 0x90, 0xfd, 0x58, 0x84,
            0x6e, 0xef, 0x29, 0xc9, 0x82, 0x20, 0x06, 0x31, 0x68, 0x19, 0x61, 0x5c, 0x9d, 0x4e,
            0x52, 0x7b, 0x63, 0xfb, 0x41, 0x7f, 0xc5, 0x31, 0x13, 0x9c, 0xb2, 0x90, 0x7c, 0x4e,
            0x8b, 0xd5, 0x9e, 0xc2, 0x2b, 0x1f, 0x42, 0x6c, 0x92, 0xa0, 0xb4, 0xd8, 0xb5, 0xdf,
            0xd2, 0x16, 0x6f, 0xe0, 0xf6, 0x25, 0xaa, 0xa6, 0xbb, 0x73, 0x85, 0x74, 0x08, 0xa6,
            0x78, 0xda, 0xfd, 0xd0, 0xc1, 0xdb, 0xa9, 0x07, 0x6d, 0x61, 0x27, 0x6d, 0x8b, 0x54,
            0xca, 0x6f, 0x10, 0x18, 0x56, 0xaf, 0xef, 0xc5, 0xb1, 0xb0, 0xd7, 0x1d, 0x3c, 0x0c,
            0x50, 0x5d, 0xb8, 0xb8, 0x6f, 0x92, 0x0d, 0xe8, 0x2a, 0x5a, 0x55, 0xff, 0x87, 0x9e,
            0xa9, 0x5d, 0x22, 0x91, 0x57, 0x57, 0xf6, 0x19, 0x76, 0xd7, 0x09, 0x4a, 0xa8, 0x58,
            0x8d, 0x9e, 0xb9, 0xb1, 0x4f, 0xa1, 0xb5, 0x23, 0x70, 0x47, 0x0d, 0x7a, 0xd1, 0xf5,
            0xca, 0xa4, 0xec, 0xf5, 0xdb, 0x88,
        ],
    };

    #[test]
    fn parse_valid() {
        let sig = Signature::try_from(VALID).expect("Could not parse valid CSS file");
        assert_eq!(VALID_PARSED, sig);

        assert_eq!(EnclaveVendor::Other, sig.vendor());
        assert_eq!(0x20200128, sig.date());
        assert_eq!(3, sig.pubkey_exponent());
        assert_eq!(0, sig.misc_select());
        assert_eq!(0xffff_ffff, sig.misc_mask());
        assert_eq!(
            0x0000_0000_0000_0003_0000_0000_0000_0004u128,
            sig.attributes()
        );
        assert_eq!(
            0xffff_ffff_ffff_ff1b_ffff_ffff_ffff_fffdu128,
            sig.attribute_mask()
        );
        assert_eq!(
            [
                0x7e, 0xe5, 0xe2, 0x9d, 0x74, 0x62, 0x3f, 0xdb, 0xc6, 0xfb, 0xf1, 0x45, 0x4b, 0xe6,
                0xf3, 0xbb, 0x0b, 0x86, 0xc1, 0x23, 0x66, 0xb7, 0xb4, 0x78, 0xad, 0x13, 0x35, 0x3e,
                0x44, 0xde, 0x84, 0x11,
            ],
            sig.mrsigner()
        );
        assert_eq!(&1, sig.product_id().as_ref());
        assert_eq!(&1, sig.version().as_ref());
    }
}
